package com.bovink.util.sample;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bovink.util.library.PoweredFooterView;
import com.bovink.util.library.PoweredRecyclerView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements PoweredRecyclerView.OnBottomLoadListener, PoweredRecyclerView.OnPullDownRefreshListener {
    @BindView(R.id.ll_main)
    LinearLayout ll_main;
    /**
     * 数据适配器，需要继承PoweredAdapter
     */
    MyPoweredAdapter adapter;
    /**
     * 当前数据页数
     */
    private int page = 1;
    /**
     * 数据总页数
     */
    private int totalPage = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        // 可用代码布局，也可直接在XML中使用
        PoweredRecyclerView poweredRecyclerView = new PoweredRecyclerView(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        );
        ll_main.addView(poweredRecyclerView, params);
        // 实现接口
        poweredRecyclerView.setOnPullDownRefreshListener(this);
        poweredRecyclerView.setOnBottomLoadListener(this);

        // 顶部
        View header = LayoutInflater.from(this).inflate(R.layout.view_recycler_header, poweredRecyclerView, false);
        RecyclerViewHeader recyclerViewHeader = new RecyclerViewHeader(header);
        // 尾部
        View footer = LayoutInflater.from(this).inflate(R.layout.view_recycler_footer, poweredRecyclerView, false);
        RecyclerFooterView recyclerFooterView = new RecyclerFooterView(footer);

        // 数据
        ArrayList<String> strings = new ArrayList<>();
        // 数据适配器
        adapter = new MyPoweredAdapter(this, strings);
        // 根据需求添加顶部与尾部
        adapter.setHeader(recyclerViewHeader);
        adapter.setFooter(recyclerFooterView);
        poweredRecyclerView.setAdapter(adapter);

        addData();
    }

    /**
     * 添加数据
     */
    private void addData() {
        // 必须设定的状态，确定是否能继续底部加载
        adapter.setHasMoreItem(page < totalPage);

        // 新数据
        ArrayList<String> list = new ArrayList<>();
        if (page == 1) {
            for (int i = 0; i < 10; i++) {
                list.add("苹果");
            }
        } else if (page == 2) {
            for (int i = 0; i < 10; i++) {
                list.add("香蕉");
            }
        }

        // 添加数据
        adapter.addStrings(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        adapter.clearStrings();
        page = 1;

        addData();
    }

    @Override
    public void onLoad() {
        page += 1;

        addData();
    }

    /**
     * 头部
     */
    private class RecyclerViewHeader extends android.support.v7.widget.RecyclerView.ViewHolder {

        public RecyclerViewHeader(View itemView) {
            super(itemView);
        }
    }

    /**
     * 尾部
     */
    class RecyclerFooterView extends PoweredFooterView {
        @BindView(R.id.tv_footer)
        TextView tv_footer;

        public RecyclerFooterView(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void loading() {
            tv_footer.setText("加载中");
        }

        @Override
        public void loadEnd() {
            tv_footer.setText("加载完毕");
        }
    }

}
